Ext.namespace("myapp.widgets");

myapp.widgets.XmasProductsTree = function(myId, myLabels, myConfig) {
   myapp.widgets.XmasProductsTree.superclass.constructor.call(this, myId, myLabels, myConfig);
}


Ext.extend(myapp.widgets.XmasProductsTree, myapp.widgets.ProductsTree, {

    initLoader : function() {
        var loader = new myapp.widgets.XmasProductsTreeLoader({
               labels: this.labels,
               id: this.id + "_treeLoader",
               dataSrc: this.config.dataSrc
        });

        loader.on("treeIsNotEmpty", function() {
            var result = this.treeIsNotEmptyHandler();
            this.fireEvent("treeIsNotEmpty", result);
        }, this);

        return loader;
    },

    refresh : function() {
       this.tree.collapseAll();
       this.tree.expandAll();
    }

});


myapp.widgets.XmasProductsTreeLoader = function(myConfig) {
   myapp.widgets.XmasProductsTreeLoader.superclass.constructor.call(this, myConfig);
}

Ext.extend(myapp.widgets.XmasProductsTreeLoader, myapp.widgets.ProductsTreeLoader, {

   buildNodes : function(parentNode) {
      var successFn = function(data) {
         var children = eval(data);

         for (var i = 0; i < children.length; i++) {
            var childNode = new Ext.tree.TreeNode({
               text: children[i].label,
               label: children[i].label,
               unitPrice: children[i].unitPrice,
               quantitiy: children[i].quantity
            });

            parentNode.appendChild(childNode);
         }
      }

      var failureFn = function() {
      }

      Ext.Ajax.request({
         url: this.config.dataSrc,
         success: successFn,
         failure: failureFn,
         params: {
		parent: parentNode.attributes.label,
		season: 'chistmas'
	 }
      });
   }
});


